//emulates an Alert dialog, in DHTML
function Alert(msg, title)
{
    var button = "<INPUT TYPE='button' NAME='button' value='OK' class='button' onclick='result(\"alert\",\"ok\")'>";
    var inner = "<table class='innerTable'><tr><td class= 'inner1'>!</td><td class='inner2'>"+msg+"</td></tr><tr><td class= 'inner3'></td><td class='inner4'>"+button+"</td></tr></table>";
    Popup( title, inner, true, 'alert');
}

//emulates an confirm dialog in DHTML
function Confirm(msg, title)
{
    var buttonCancel = "<INPUT TYPE='button' NAME='button' value='Cancel' class='button' onClick='result(\"confirm\",\"cancel\")'>";
    var buttonOK = "<INPUT TYPE='button' NAME='button' value='OK' class='button' onClick='result(\"confirm\",\"ok\")'>";
    var inner = "<table class='innerTable'><tr><td class= 'inner1'>!</td><td class='inner2'>"+msg+"</td></tr><tr><td class= 'inner3'></td><td class='inner4'>"+buttonCancel+"&nbsp"+buttonOK+"</td></tr></table>";
    Popup(title, inner, true, 'confirm');
}

//emulates a prompt dialog in DHTML
function Prompt(msg, title)
{
    var input = '<input type="text" id ="text" class ="text">';
    var buttonCancel = "<INPUT TYPE='button' NAME='button' value='Cancel' class='button' onClick='result(\"prompt\",null)'>";
    var buttonOK = "<INPUT TYPE='button' NAME='button' value='OK' class='button' onClick='result(\"prompt\",document.getElementById(\"text\").value)'>";
    var inner = "<table class='innerTable'><tr><td class= 'inner1'>!</td><td class='inner2'>"+msg+"<br><br>"+input+"</td></tr><tr><td class= 'inner3'></td><td class='inner4'>"+buttonCancel+"&nbsp"+buttonOK+"</td></tr></table>";
    Popup( title, inner, true, 'prompt');
}

// Get the result of JsPrompt
function result(component, data)
{
	closePopup(component);
    promptResult(component, data);
}

function closePopup(id, callbackFunction)
{
	if ( element = document.getElementById('popupContainer'+id) )
	{
		element.parentNode.removeChild( element );
	}

	if (element = document.getElementById('popupBack') )
	{
		element.parentNode.removeChild( element );
	}
}


//Create the jsprompt dialog
function Popup( title, inner, modal, id )
{
	//default values
    if (!title )
    {
        title = 'Popup';
    }
    if ( !inner )
    {
        inner = '';
    }
    
    var divprompt 				= document.createElement("div");
    divprompt.id				= "popupContainer"+id;
    divprompt.className			= "popupContainer";
    divprompt.innerHTML			= "<table id='popupTable"+id+"' class='popupTable' style='position:absolute'><tr><td class= 'table10'></td><td class='table11' onmousedown='dragStart(event,\"popupTable"+id+"\")' >"+title+"</td><td class='table12' onclick='closePopup(\""+id+"\")'></td></tr><tr><td class= 'table20'></td><td class='table21'>"+inner+"</td><td class='table22'></td></tr><td class= 'table30'></td><td class='table31'></td><td class='table32'></td></tr></table>";
    divprompt.style.top         = parseInt((window.innerHeight/4));
    divprompt.style.left        = parseInt((window.innerWidth/3));
    divprompt.style.position    = 'fixed';
    document.body.appendChild(divprompt);

    //only put if it don't is in document
    if ( modal && ! document.getElementById('popupBack' ))
    {
        var divback 		= document.createElement("div");
        divback.id			= "popupBack";
        divback.className	= "popupBack";
        document.body.appendChild(divback );
    }
    
    return id;
}
